package com.sp2p.service.admin;

import java.sql.Connection;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.shove.base.BaseService;
import com.shove.data.DataException;
import com.shove.data.dao.MySQL;
import com.shove.vo.PageBean;
import com.sp2p.constants.IConstants;
import com.sp2p.dao.OperationLogDao;
import com.sp2p.dao.admin.StockFinancingManageDao;
import com.sp2p.service.FinanceService;

public class StockFinancingManageService extends BaseService {

	public static Log log = LogFactory.getLog(FinanceService.class);

	private StockFinancingManageDao stockFinancingManageDao;
	private OperationLogDao operationLogDao;
	
	public void setStockFinancingManageDao(
			StockFinancingManageDao stockFinancingManageDao) {
		this.stockFinancingManageDao = stockFinancingManageDao;
	}

	public void setOperationLogDao(OperationLogDao operationLogDao) {
		this.operationLogDao = operationLogDao;
	}



	public void queryFinancingUserInfoList(PageBean<Map<String, Object>> pageBean,
			String userName,String realName,Integer level, String startTime, String endTime, Integer status)
			throws Exception {
		Connection conn = MySQL.getConnection();
		String resultFeilds = " * , f_sjzd('pzInfoStatusType',infoStatus) infoStatusRename ";
		String command = "";
		if (StringUtils.isNotBlank(userName)) {
			command += " and username like '%"
					+ StringEscapeUtils.escapeSql(userName) + "%' ";
		}
		if (StringUtils.isNotBlank(realName)) {
			command += " and realName like '%"
					+ StringEscapeUtils.escapeSql(realName) + "%' ";
		}
		if (level > 0) {
			command += " and level =" + level;
		}
		if (status > 0) {//
			command += " and infoStatus =" + status;
		}
		if (StringUtils.isNotBlank(startTime)) {
			command += " and applyTime >='"
					+ StringEscapeUtils.escapeSql(startTime) + "'";
		}
		if (StringUtils.isNotBlank(endTime)) {
			command += " and applyTime <='"
					+ StringEscapeUtils.escapeSql(endTime) + "'";
		}
		try {
			dataPage(conn, pageBean, "v_pz_user_info", resultFeilds, " order by applyTime desc",
					command);
			conn.commit();
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			conn.rollback();
				
			throw e;
		} finally {
			conn.close();
		}
	}
	
	public Map<String, String> queryFinancingUserInfoCheck(Long id) throws Exception,
	DataException {
		Connection conn = MySQL.getConnection();
		try {
			return stockFinancingManageDao.queryFinancingUserInfoCheck(conn, id);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		
			throw e;
		} finally {
			conn.close();
		}
	}
	public Long updateFinancingUserInfoCheck(Long id, Integer status, String homsId,
			String homsPwd, String username, String lastIP) throws Exception {
		Connection conn = MySQL.getConnection();
		long resultId = -1L;
		try {

			resultId = stockFinancingManageDao.updatePzUserInfo(conn, id, status,
					homsId, homsPwd);
			if (resultId > 0) {
				operationLogDao.addOperationLog(conn, "t_bankcard", username,
						IConstants.UPDATE, lastIP, 0, "股票配资审核", 2);
			}
			conn.commit();
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
			conn.rollback();

			throw e;
		} finally {
			conn.close();
		}
		return resultId;
	}
}
